<?php

class connexion{

	var $login="";
	var $mdp="";

	function __construct($login,$mdp){

	$this->login=$login;
	$this->mdp=$mdp;

	}

	function connexion(){
			
		try{
			$user = 'root';
			$password = 'root';
			$datasource = 'mysql:host=localhost;dbname=blog;charset=utf8';

			$base = new PDO ($datasource,$user,$password);
			$result = $base->query('SELECT * FROM utilisateur WHERE login=\''.$this->login.'\' AND motDePasse=\''.$this->mdp.'\'');
			$rows_returned = $result->rowCount();
				
			if($rows_returned > 0){
				$row = $result->fetch(PDO::FETCH_ASSOC); 
				$_SESSION['login']=$row['login'];
				$_SESSION['id']=$row['id'];
				$_SESSION['nom']=$row['nom'];
				$_SESSION['prenom']=$row['prenom'];
				
				
				$result = $base->query('SELECT id_droit FROM utilisateur_droit WHERE id_utilisateur=\''.$_SESSION['id'].'\'');
				$rows_returned = $result->rowCount();
				
				if($rows_returned > 0){
					$row = $result->fetch(PDO::FETCH_ASSOC); 
					$_SESSION['id_droit']=$row['id_droit'];
				}
				
				$result = $base->prepare('UPDATE utilisateur SET actif=1 WHERE id=\''.$_SESSION['id'].'\'');
				if($result->execute()){
					echo "";
				}else{
					var_dump($_SESSION);
				}
				
				
			}
			else return -1;
			
		}
		catch (PDOException $e){
			var_dump($e->getTrace());
			die("Erreur !:".$e->getMessage().", ". $e->getTraceAsString());
		}	
	}
}

?>